# This Python file uses the following encoding: utf-8
import math
import sys
import  os
from PySide6.QtWidgets import QApplication, QDialog
from ui_form import Ui_Dialog

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.ok.clicked.connect(self.calculate)
        self.ui.clear.clicked.connect(self.clear_all)
        self.ui.readme.clicked.connect(self.read_me)

    def calculate(self):
        """陀螺定向计算"""
        self.ui.image.hide()
        self.ui.output.setText("")
        up1, up2,up3 = self.angle(float(self.ui.cord11.text()), float(self.ui.cord12.text()), float(self.ui.cord21.text()),
                              float(self.ui.cord22.text()))
        gama_up,gama_up_a,gama_1_second=self.d_angle(float(self.ui.cord11.text()), float(self.ui.cord12.text()))
        self.ui.output.setText("地面测站点子午线收敛角为："+gama_up_a)

        down1, down2,down3 = self.angle(float(self.ui.cord31.text()), float(self.ui.cord32.text()),
                                  float(self.ui.cord41.text()), float(self.ui.cord42.text()))
        gama_down, gama_down_a,gama_2_second = self.d_angle(float(self.ui.cord31.text()), float(self.ui.cord32.text()))
        up_final_secnod=up3+gama_1_second
        down_final_secnod = down3 + gama_2_second
        up_final=up_final_secnod/3600
        down_final=down_final_secnod/3600

        aa21 = int(up_final)
        if aa21<0:
            aa21=aa21+360
        aa22 = int((up_final - aa21) * 60)
        aa23 = round(round((up_final - aa21 - aa22 / 60) * 3600, 1))
        up_final_angle = str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"  # °′″形式
        ###################
        aa21 = int(down_final)
        if aa21<0:
            aa21=aa21+360
        aa22 = int((down_final - aa21) * 60)
        aa23 = round(round((down_final - aa21 - aa22 / 60) * 3600, 1))
        down_final_angle = str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"  # °′″形式

        self.ui.output.setText(self.ui.output.toPlainText()+"\n"+"地下测站点子午线收敛角为：" +  gama_down_a)
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地面观测线坐标反算方位角：" + up2 )
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地下观测线坐标反算方位角：" + down2 )
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地面观测线真方位角计算值：" + up_final_angle  )
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地下观测线真方位角计算值：" + down_final_angle  + "\n")
        # -----------------------------------------------------------------------------------------------------------------------------------------------------
        ave_up_11,ave_up_angle_11,s11,ave_up_11_delta=self.tl_angle(int(self.ui.angle11.text()), int(self.ui.angle12.text()))
        ave_up_12, ave_up_angle_12,s12, ave_up_12_delta = self.tl_angle(int(self.ui.angle21.text()),int(self.ui.angle22.text()))
        self.ui.output.setText(self.ui.output.toPlainText()+"\n"+"①地面第一测回陀螺方位角：" + ave_up_angle_11)
        self.ui.output.setText(self.ui.output.toPlainText()+"\n"+"①地面第二测回陀螺方位角：" + ave_up_angle_12)
        ave_1_ave=(ave_up_11+ave_up_12)/2
        aa21 = int(ave_1_ave)
        aa22 = int((ave_1_ave - aa21) * 60)
        aa23 = round(round((ave_1_ave - aa21 - aa22 / 60) * 3600,1))
        ave_1_ave_angle = str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"  # °′″形式
        ave_1_second=aa21*3600+aa22*60+aa23                        #″形式
        d_1_long=s12-s11
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "①两测回陀螺方位角的互差：" + str(d_1_long)+ "″")
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "①两测回陀螺方位角的均值：" + ave_1_ave_angle + "\n" )
        # -------------------------------------------------------------------------------------------------------------------------------------------------------
        ave_up_21, ave_up_angle_21, s21,ave_up_21_delta = self.tl_angle(int(self.ui.angle31.text()),
                                                                    int(self.ui.angle32.text()))
        ave_up_22, ave_up_angle_22,s22,ave_up_22_delta = self.tl_angle(int(self.ui.angle41.text()),
                                                                    int(self.ui.angle42.text()))
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "②地下第一测回陀螺方位角：" + ave_up_angle_21)
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "②地下第二测回陀螺方位角：" + ave_up_angle_22)
        ave_2_ave = (ave_up_21 + ave_up_22) / 2
        aa21 = int(ave_2_ave)
        aa22 = int((ave_2_ave - aa21) * 60)
        aa23 = round(round((ave_2_ave - aa21 - aa22 / 60) * 3600,1))
        ave_2_ave_angle = str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"  # °′″形式
        ave_2_second=aa21*3600+aa22*60+aa23
        d_2_long=s22-s21
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "②两测回陀螺方位角的互差：" + str(d_2_long) + "″")
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "②两测回陀螺方位角的均值：" + ave_2_ave_angle+ "\n" )
        # --------------------------------------------------------------------------------------------------------------------------------------------------------
        ave_up_31, ave_up_angle_31, s31,ave_up_31_delta = self.tl_angle(int(self.ui.angle51.text()),
                                                                    int(self.ui.angle52.text()))
        ave_up_32, ave_up_angle_32, s32,ave_up_32_delta = self.tl_angle(int(self.ui.angle61.text()),
                                                                    int(self.ui.angle62.text()))
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "③地面第一测回陀螺方位角：" + ave_up_angle_31)
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "③地面第二测回陀螺方位角：" + ave_up_angle_32)
        ave_3_ave = (ave_up_31 + ave_up_32) / 2
        aa21 = int(ave_3_ave)
        aa22 = int((ave_3_ave - aa21) * 60)
        aa23 = round(round((ave_3_ave - aa21 - aa22 / 60) * 3600,1))
        ave_3_ave_angle = str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"  # °′″形式
        ave_3_second = aa21 * 3600 + aa22 * 60 + aa23  # ″形式
        d_3_long=s32-s31
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "③两测回陀螺方位角的互差：" + str(d_3_long) + "″")
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "③两测回陀螺方位角的均值：" + ave_3_ave_angle+ "\n" )
        # --------------------------------------------------------------------------------------------------------------------------------------------------------
        d_ave=ave_3_second-ave_1_second
        ave_up_second=round(round((ave_3_second+ave_1_second)/2,1))
        ave_up=ave_up_second/3600
        aa21 = int(ave_up)
        aa22 = int((ave_up - aa21) * 60)
        aa23 = round(round((ave_up - aa21 - aa22 / 60) * 3600,1))
        ave_up_angle= str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"  # °′″形式
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地面两次观测陀螺方位角差：" + str(d_ave)+"″" )
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地面观测陀螺方位角的均值：" + ave_up_angle )
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地下观测陀螺方位角的均值：" + ave_2_ave_angle )
        d_A=up_final_secnod-ave_up_second #仪器常数
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "求得陀螺全站仪的仪器常数：" +str(d_A)+"″" )

        a1=ave_up_second-s11
        a2=ave_up_second-s12
        a3=ave_up_second-s31
        a4=ave_up_second-s32
        a_all=a1*a1+a2*a2+a3*a3+a4*a4
        one_delta_ave=round(math.sqrt(a_all/3),1)
        # -----------------------------------------------------------------------------------------------------------------------------------------------------------
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "陀螺方位角一次测定中误差：±" + str(one_delta_ave)+"″" + "\n")
        final_result_second=ave_2_second -gama_2_second  +d_A
        final_result=final_result_second/3600
        aa21 = int(final_result)
        aa22 = int((final_result - aa21) * 60)
        aa23 = round(round((final_result - aa21 - aa22 / 60) * 3600,1))
        final_result_angle= str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"  # °′″形式
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "求得地下观测线坐标方位角："+final_result_angle)
        d_result=final_result_second-down3
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "地下观测线方位角的修正量：" +str(d_result)+"″")
        d_error=round(math.sqrt((2+4)/(2*4))*one_delta_ave,1)
        self.ui.output.setText(self.ui.output.toPlainText() + "\n" + "本次定向的一次定向中误差：±" +  str(d_error)+"″")


    def angle(self, x1, y1, x2, y2):
        """坐标反算"""
        dx = x2 - x1
        dy = y2 - y1
        a1 = math.atan2(dx, dy) * 180 / math.pi
        a2 = -(a1 - 90)
        if a2 < 0:
            a2 = a2 + 360
        aa21 = int(a2)
        if aa21<0:
            aa21=aa21+360
        aa22 = int((a2 - aa21) * 60)
        aa23 = round((a2 - aa21 - aa22 / 60) * 3600)
        aa2 = str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"
        aa2_second=aa21*3600+aa22*60+aa23
        return a2, aa2,aa2_second

    def d_angle(self, x, y):
        """子午线收敛角计算"""
        text = [[0,0,0],
                [100, 0.0085, 85],
                [200, 0.017, 85],
                [300, 0.0255, 86],
                [400, 0.0341, 85],
                [500, 0.0426, 83],
                [600, 0.0512, 86],
                [700, 0.0598, 86],
                [800, 0.0684, 87],
                [900, 0.0771, 87],
                [1000, 0.0858, 87],
                [1100, 0.0945, 88],
                [1200, 0.1033, 88],
                [1300, 0.1121, 89],
                [1400, 0.121, 90],
                [1500, 0.13, 90],
                [1600, 0.139, 91],
                [1700, 0.1481, 92],
                [1800, 0.1573, 93],
                [1900, 0.1666, 95],
                [2000, 0.1759, 95],
                [2100, 0.1854, 97],
                [2200, 0.1949, 97],
                [2300, 0.2046, 99],
                [2400, 0.2143, 100],
                [2500, 0.2242, 102],
                [2600, 0.2342, 103],
                [2700, 0.2444, 104],
                [2800, 0.2547, 107],
                [2900, 0.2651, 107],
                [3000, 0.2753, 107],
                [3100, 0.2865, 110],
                [3200, 0.2975, 111],
                [3300, 0.3086, 114],
                [3400, 0.32, 116],
                [3500, 0.3316, 118],
                [3600, 0.3434, 120],
                [3700, 0.3554, 123],
                [3800, 0.3677, 125],
                [3900, 0.3802, 129],
                [4000, 0.3931, 131],
                [4100, 0.4062, 134],
                [4200, 0.4196, 138],
                [4300, 0.4334, 141],
                [4400, 0.4475, 144],
                [4500, 0.4619, 149],
                [4600, 0.4768, 153],
                [4700, 0.4921, 157],
                [4800, 0.5078, 162],
                [4900, 0.524, 167],
                [5000, 0.5407, 172],
                [5100, 0.5579, 178],
                [5200, 0.5757, 184],
                [5300, 0.5941, 190],
                [5400, 0.6131, 197],
                [5500, 0.6328, 205],
                [5600, 0.6533, 212],
                [5700, 0.6745, 222],
                [5800, 0.6967, 230],
                [5900, 0.7197, 240],
                [6000, 0.7437, 248],
                ]
        k=float(text[int(x/100000)][1])+float(text[int(x/100000)][2])/10000*((x-int(x/100000)*100000)/100000)
        a1=k*(float(str(y)[2:])/1000-500)/60
        a2=abs(a1)
        if a1>0:
            num=1
        else:
            num=0
        aa21 = int(a2)
        aa22 = int((a2 - aa21) * 60)
        aa23 = round((a2 - aa21 - aa22 / 60) * 3600)
        aa2 = str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"
        aa2_second=aa21*3600+aa22*60+aa23
        if num==0:
            aa2="-"+aa2
            aa2_second=-aa2_second
        return a1,aa2,aa2_second

    def tl_angle(self,a1,a2):
        """一测回陀螺方位角计算"""
        a1=str(a1)
        a2=str(a2)
        if len(a1)<7:
            for i in range(7-len(a1)):
                a1=str("0")+a1
        if len(a2)<7:
            for i in range(7-len(a2)):
                a2=str("0")+a2
        a11=float(a1[0:3])
        a12=float(a1[3:5])
        a13=float(a1[5:])
        a21=float(a2[0:3])
        a22=float(a2[3:5])
        a23=float(a2[5:])
        aa1=a11+a12/60+a13/3600
        if float(a11)>float(a21):
            aa2=a21+180+a22/60+a23/3600
        else:
            aa2 = a21 -180 + a22 / 60 + a23 / 3600
        delta=(aa2-aa1)*3600    #单位为″
        ave=(aa1+aa2)/2     #一测回陀螺方位角，小数形式
        aa21 = int(ave)
        aa22 = int((ave - aa21) * 60)
        aa23 = round(round((ave - aa21 - aa22 / 60) * 3600,1))
        ave_angle= str(aa21) + "°" + str(aa22) + "′" + str(aa23) + "″"      #°′″形式
        ave_angle_second=aa21*3600+aa22*60+aa23           #″形式
        return  ave, ave_angle,ave_angle_second, delta
    def clear_all(self):
        self.ui.angle11.setText("")
        self.ui.angle12.setText("")
        self.ui.angle21.setText("")
        self.ui.angle22.setText("")
        self.ui.angle31.setText("")
        self.ui.angle32.setText("")
        self.ui.angle41.setText("")
        self.ui.angle42.setText("")
        self.ui.angle51.setText("")
        self.ui.angle52.setText("")
        self.ui.angle61.setText("")
        self.ui.angle62.setText("")
        self.ui.output.setText("")
        self.ui.cord11.setText("")
        self.ui.cord12.setText("")
        self.ui.cord21.setText("")
        self.ui.cord22.setText("")
        self.ui.cord31.setText("")
        self.ui.cord32.setText("")
        self.ui.cord41.setText("")
        self.ui.cord42.setText("")
        self.ui.image.show()

    def read_me(self):
        os.startfile("https://southern-saxophone-c79.notion.site/b1a82dbbf6984ac4bb9e2328c08572e6")
if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = Dialog()
    widget.show()
    sys.exit(app.exec())
